package lu.med.praxiswahl.wahlumed.client.gravtest;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;

import lu.med.praxiswahl.wahlumed.client.utilities.ColumnWidthControler;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;

@SuppressWarnings("serial")
public class GravTestHalfTable extends JTable implements GravTestTableListener {
	private GravTestHalfModel model;

	private ColumnWidthControler c;
	private boolean isColumnWidthChanged;
	private TableColumn resizedColumn;

	public GravTestHalfTable(GravTestHalfModel model, GravTestDataModel umodel) {
		super(model);
		this.model = model;
		umodel.addGravTestTableListener(this);
		this.setTableHeader(tableHeader);
		this.setRowHeight(30);
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		// this.setAutoResizeMode(AUTO_RESIZE_OFF);
		this.selectionModel = getSelectionModel();
		this.selectionModel
				.addListSelectionListener(new GravTestHalfTableSelectionListener(
						model));
		this.setDefaultRenderer(Object.class, new GravTestHalfCellRenderer(
				model));
		this.setDefaultEditor(Object.class, new GravTestHalfCellEditor(model,
				null));

		TableColumn column = null;

		c = new ColumnWidthControler(Main.COLUMNWIDTH_CONFIGURATION_FILE,
				"gravTesttbl", this);

		for (int i = 0; i < 4; i++) {
			column = this.getColumnModel().getColumn(i);
			column.setPreferredWidth(c.getColumnWidth(i));
		}
		this.getColumnModel().addColumnModelListener(
				new TableColumnWidthListener(this));
		this.getTableHeader().addMouseListener(
				new TableHeaderMouseListener(this, c));
	}

	@Override
	public void refresh() {
		model.refresh();
		this.repaint();
	}

	public boolean getColumnWidthChanged() {
		return isColumnWidthChanged;
	}

	public void setColumnWidthChanged(boolean widthChanged,
			TableColumn resizedColumn) {
		isColumnWidthChanged = widthChanged;
		this.resizedColumn = resizedColumn;
	}

	public TableColumn getResizedColumn() {
		return resizedColumn;
	}

}
